//
//  HLHomeListVideoView.m
//  YueYue
//
//  Created by hulu001 on 2023/5/25.
//

#import "HLHomeListVideoView.h"
#import "HLMatchGoldViewController.h"
#import "UIScrollView+ScrollAnimation.h"

@interface HLHomeListVideoView ()<UICollectionViewDelegate,UICollectionViewDataSource, UIScrollViewDelegate>

@property (nonatomic, strong) HLHomeRecommendRequest *recommendRequest;
@property (nonatomic, strong) NSArray <HLHomeListModel*>*videos;
@property (nonatomic, strong) UIImageView *backView;
@property (nonatomic, strong) NSString *timerRcommendKey;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;
@property (nonatomic, assign) NSInteger totalItemCount;
@property (nonatomic, assign) BOOL isLeft;
@property (nonatomic, assign) BOOL isRequesting;

@end

@implementation HLHomeListVideoView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = UIColorWhite;
        [self setupView];
        
    }
    return self;
}

- (void)setupView {
    
    self.backView = [[UIImageView alloc] initWithImage:UIImageMake(@"home_video_back")];
    [self addSubview:_backView];
    [_backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    
    UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.minimumLineSpacing = 8;
    flowLayout.minimumInteritemSpacing = 0;
    
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    flowLayout.itemSize = CGSizeMake(80, 100);
        //设置代理，实现代理方法
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    _collectionView.pagingEnabled = NO;
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.backgroundColor = UIColorClear;
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [_collectionView registerClass:[HLHomeListVideoCell class] forCellWithReuseIdentifier:[HLHomeListVideoCell identifyCell]];
    [self addSubview:_collectionView];
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(16);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(100);
        make.left.mas_equalTo(16);
        make.bottom.mas_equalTo(-16);
    }];
}


-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.totalItemCount;
    
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    long itemIndex = indexPath.item % _videos.count;
    
    HLHomeListVideoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[HLHomeListVideoCell identifyCell] forIndexPath:indexPath];
    cell.model = self.videos[itemIndex];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    long itemIndex = indexPath.item % _videos.count;
    HLHomeListModel *video = self.videos[itemIndex];
    HLUserInfoModel *user = [[HLUserInfoModel alloc] init];
    user.userId = video.userId;
    user.nickName = video.nickName;
    user.avatar = video.avatar;
    user.videoType = [video videoType];
    [HLUIHelper callWithUser:user];
}

- (void)setVideos:(NSArray<HLHomeListModel *> *)videos
{
    
    if (videos.count > 0){
        _videos = videos;
        
        self.isLeft = NO;
        self.totalItemCount = videos.count * 20;
        [self.collectionView reloadData];
        [self animateWithCollection];
        self.isRequesting = NO;
    } else {
        if (self.videos.count > 0){
            [self animateWithCollection];
        }
    }
}

- (void)animateWithCollection
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.collectionView setContentOffset: CGPointZero];
        CAMediaTimingFunction *timing = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
        NSInteger animateCount = (self.totalItemCount - 5);
        [self.collectionView setContentOffset:CGPointMake(self.collectionView.contentOffset.x + (animateCount * 88), self.collectionView.contentOffset.y) withTimingFunction:timing duration:(animateCount * 1.4)];
    });
}

- (void)setRecommendType:(HLHomeRecommendType)recommendType
{
    self.timerRcommendKey = [NSString stringWithFormat:@"home_list_video_%@",@(recommendType)];
    _recommendType = recommendType;
    self.recommendRequest = [[HLHomeRecommendRequest alloc] initWithType:_recommendType];
    [self requestRecomment];
}

- (void)requestRecomment
{
    if (self.isRequesting || (HLRunTime.shareInstance.mine == nil)) {
        return;
    }
    
    self.isRequesting = YES;
    
    [_recommendRequest startWithCompletionBlockWithSuccess:^(__kindof HLHomeRecommendRequest * _Nonnull request) {
        if (request.isSuccess){
            self.videos = [request recommendList];
            
        }
    } failure:^(__kindof HLHomeRecommendRequest * _Nonnull request) {
        
    }];
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    [self requestRecomment];
}

@end
